package com.system.service.impl;

import com.system.mapper.AddressMapper;
import com.system.mapper.ClientMapper;
import com.system.pojo.Address;
import com.system.pojo.Client;
import com.system.service.ClientService;
import com.system.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ClientMapper clientMapper;


    @Override
    public ResultVo<Client> insert(Client client) {
        ResultVo<Client> resultVo = new ResultVo<Client>();
        //1.是否有客户名重复
        String clientName = client.getClientName();
        Client client2 = clientMapper.selectByClientName(clientName);
        if (client2 == null) {
            clientMapper.insertSelective(client);
			Integer id = client.getId(); //获取新客户的id
			//客户的默认收获地址
			Address address = client.getAddress();
			if (address.getProvince() == "" && address.getCity() == ""
					&& address.getTown() == "" && address.getStreet() == "") {
				resultVo.setInsert(true);
				resultVo.setMessage("添加成功");
				return resultVo;
			}
			address.setClientId(id);
			boolean isTrue = insertClientAddress(address);
			if (isTrue){
				//插入默认地址成功
				//获取默认地址id,更新客户的默认地址id
				Integer aid = address.getId();
				Client client1 = new Client();
				client1.setId(id);
				client1.setAddressId(aid);
				clientMapper.updateByPrimaryKeySelective(client1);
				resultVo.setInsert(true);
				resultVo.setMessage("客户信息添加成功");
			}else{
				//插入默认地址失败
				resultVo.setMessage("客户信息添加失败");
			}
		}else{
        	//已存在该客户，插入失败
			resultVo.setMessage("该客户已存在");
		}
		return resultVo;

    }

	public boolean insertClientAddress(Address address){
		int i = addressMapper.insertSelective(address);
		if (i == 1){
			return true;
		}else{
			return false;
		}
	}

    @Override
    public ResultVo<Client> updateById(Client client) {
        ResultVo<Client> resultVo = new ResultVo<Client>();
        int i = clientMapper.updateByPrimaryKeySelective(client);
        if (i == 1){
            //更新成功
            resultVo.setUpdate(true);
            resultVo.setMessage("信息更新成功");
        }else{
            //更新失败
            resultVo.setMessage("信息更新失败");
        }
        return resultVo;
    }


    @Override
    public ResultVo<Client> selectAll() {
        ResultVo<Client> resultVo = new ResultVo<Client>();
        List<Client> clients = clientMapper.selectAll();
        resultVo.setTList(clients);
        resultVo.setSelect(true);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

    @Override
    public ResultVo<Client> detail(Integer clientId) {
        ResultVo<Client> resultVo = new ResultVo<Client>();
        Client client = clientMapper.selectByPrimaryKey(clientId);
        Integer defaultAddressId = client.getAddressId();
        List<Address> addresses = client.getAddresses();
        if (addresses != null){
            for (Address address : addresses) {
                if (address.getId() == defaultAddressId){
                    address.setDefaultAddress(true);
                }else{
                    address.setDefaultAddress(false);
                }
            }
        }
        resultVo.setT(client);
        resultVo.setSelect(true);
        resultVo.setMessage("查询成功");
        return resultVo;
    }

}
